package com.foreknow.demo;

import java.util.Scanner;

/**
 * 对员工Employee的操作类
 */
public class EmployeeManager {
    private Employee[] employees;
    int count;
    public EmployeeManager() {
        employees = new Employee[5];
    }

    /**
     *  登录(根据用户名和密码登录)
     *  SQL: select * from employee where name=? and pass=?
     * @param name
     * @param pass
     * @return
     */
    public Employee login(String name,String pass) {
        return null;
    }


    /**
     * 注册员工信息（编号 姓名 密码 工资  所在部门）
     * SQL: insert into employee values(?,?,?,?,?)
     * @param employee
     */
    public void register(Employee employee) {
        if(employee != null) {
            employees[count] = employee;
            count++;
            System.out.println("success......");
        }else {
            System.out.println("error......");
        }
    }

    /**
     * 根据员工的编号查询某个员工信息
     */
    public Employee getEmployeeById(String eid) {
        Employee emp = null;
        //遍历数组
        for(int i = 0;i < count;i++){
            if(employees[i] != null) {
                if(employees[i].getEid().equals(eid)) {
                    //从数组中获取对象
                    emp = employees[i];
                }
            }
        }
        return emp;
    }

    /**
     * 修改员工信息
     * 1. 先根据id查询到某个对象
     * 2. 修改对象的信息
     */
    public Employee updateEmployee(Employee employee) {
        employee.setName("xxx");
        return  employee;
    }

    /**
     * 查询所有员工信息
     * select * from employee
     */
    public Employee[] getAll() {
        //定义一个新的数组容器，然后对原数组employees遍历并将原数组中的元素放到新数组中
        Employee[] emps = new Employee[5];
        for(int i = 0;i < employees.length;i++){
            emps[i] = employees[i];
        }
        return emps;
    }
    public static void main(String[] args) {
        //        Scanner input = new Scanner(System.in);
        //        System.out.println("请输入员工的编号：");
        //        String eid = input.next();
        //        System.out.println("请输入员工的姓名：");
        //        String name = input.next();
        //        System.out.println("请输入员工的密码：");
        //        String pass = input.next();
        //        System.out.println("请输入员工的工资：");
        //        double salary = input.nextDouble();
        //        System.out.println("请输入员工的所在部门：");
        //        String dept = input.next();

        EmployeeManager employeeManager = new EmployeeManager();
        Employee e1 = new Employee("10001","fender1","123456",5000.50,"market");
        Employee e2 = new Employee("10002","fender2","123456",5000.50,"market");
        Employee e3 = new Employee("10003","fender3","123456",5000.50,"market");
        Employee e4 = new Employee("10004","fender4","123456",5000.50,"market");
        Employee e5 = new Employee("10005","fender5","123456",5000.50,"market");
        //调用注册的方法
        employeeManager.register(e1);
        employeeManager.register(e2);
        employeeManager.register(e3);
        employeeManager.register(e4);
        employeeManager.register(e5);

        //查询所有员工信息
        Employee[] e = employeeManager.getAll();
        for(int i = 0;i < e.length;i++){
            Employee employee = e[i];
            System.out.println(employee.getName());
        }

        //调用getEmployeeById方法
        //EmployeeManager employeeManager1 = new EmployeeManager();
        Employee emps = employeeManager.getEmployeeById("10003");
        System.out.println(emps.getEid()+"---"+emps.getName());
        //调用修改的方法
        Employee updateEmps = employeeManager.updateEmployee(emps);
        System.out.println(updateEmps.getName());
    }
}
